/**
 * axios封装
 * 请求拦截、响应拦截、错误统一处理
 */

export const baseurl = "http://localhost:9090"; // 本地
// export const baseurl = 'http://121.37.242.119:9090' // Linux服务器
// const baseurl = 'http://43.139.147.77:9090' // Windows服务器

const request = (url, data = {}, method = "GET") => {
  const token = uni.getStorageSync("token");
  const userId = uni.getStorageSync("userId");

  if (token) {
    return new Promise((resolve, reject) => {
      uni.request({
        url: baseurl + url, //接口地址
        data,
        method,
        header: {
          "content-type": "application/json",
          authentication: token,
          userId: userId,
        },
        success: (res) => {
          resolve(res.data);
        },
        fail: (res) => {
          console.error(res);
          reject(res);
        },
      });
    });
  } else {
    uni.showToast({
      title: "请先登录",
      icon: "none",
    });
    uni.navigateTo({
      url: "/pages/login/login",
    });
  }
};

export default request;
